<?php
class article extends core {
	public $article_mod;
	
	public function __construct() {
		parent::__construct();
		$this->article_mod = m('article');
	}

	//index
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$category_id = (isset($_GET['category_id']) && trim($_GET['category_id'])) ? trim($_GET['category_id']) : '';
        $ext_property = isset($_GET['ext_property']) ? $_GET['ext_property'] : '';
		if ($id) {
			$where .= " AND a.id='{$id}'";
		}
		if ($keyword) {
			$where .= " AND (a.title LIKE '%{$keyword}%' OR a.content LIKE '%{$keyword}%')";
		}
		if ($category_id) {
			$where .= " AND a.category_id='{$category_id}'";
		}
		if ($ext_property) {
			if (!is_array($ext_property)) $ext_property = explode(',', $ext_property);
			$where .= " AND (";
			foreach ($ext_property as $e) {
				$where .= "CONCAT(',',g.ext_property,',') LIKE '%,{$e},%' OR ";
			}
			$where = trim($where, ' OR ').")";
			$ext_property = implode(',',$ext_property);
		}
		//分页
		$this->setpages(array('id'=>$id, 'keyword'=>$keyword, 'category_id'=>$category_id, 'ext_property'=>$ext_property));

		$sql = "SELECT a.*, ac.name as category_name FROM {$this->tbp}article a 
			LEFT JOIN {$this->tbp}article_category ac ON a.category_id=ac.id 
			WHERE 1 {$where} ORDER BY is_system ASC, a.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('category_id',$category_id);
		$this->smarty->assign('ext_property',$ext_property);
		$this->smarty->assign('categorys', $this->article_mod->categorys());
		$this->display();
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$this->smarty->assign('categorys', $this->article_mod->categorys());
			$this->display();
		}
	}

	//add
	public function edit() {
		if (IS_POST) { //添加
			$this->save();
		} else { //显示
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}article WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);

			$sql = "SELECT * FROM {$this->tbp}article_pic WHERE article_id='{$id}'";
			$pics = $this->db->get_results($sql);
			$this->smarty->assign('pics', $pics);

			$sql = "SELECT g.* FROM {$this->tbp}goods g LEFT JOIN {$this->tbp}article_goods ag ON ag.goods_id=g.id WHERE ag.article_id='{$id}'";
			$goods = $this->db->get_results($sql);
			$this->smarty->assign('goods', $goods);
			
			$this->smarty->assign('categorys', $this->article_mod->categorys());
			
			$sql = "SELECT c.*, m.avatar, m.name as member_name, m.nick_name as member_nick_name
				FROM {$this->tbp}article_comment c LEFT JOIN {$this->tbp}member m ON c.member_id=m.id 
				WHERE article_id='{$id}' AND c.parent_id=0 ORDER BY c.id ASC";
			$comments = $this->db->get_results($sql);
			if ($comments) {
				foreach ($comments as $k=>$g) {
					$comments[$k]->add_time = get_time_word($g->add_time);
					$sql = "SELECT c.id, c.member_id, c.content, c.add_time, m.name as member_name FROM {$this->tbp}article_comment c 
						LEFT JOIN {$this->tbp}member m ON c.member_id=m.id 
						WHERE c.parent_id='{$g->id}' ORDER BY c.id ASC";
					$replys = $this->db->get_results($sql);
					if ($replys) {
						foreach ($replys as $rk=>$rg) {
							$replys[$rk]->add_time = get_time_word($rg->add_time);
							if ($rg->member_id==0) $replys[$rk]->member_name = '客服';
						}
					}
					$comments[$k]->replys = $replys;
				}
			}
			$this->smarty->assign('comments', $comments);

			$this->display();
		}
	}

	public function upload_pic(){
        $result = upload_one_file('article', 'pic');
        echo $result;
    }

	//写入到数据库
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$category_id = (isset($_POST['category_id']) && trim($_POST['category_id'])) ? intval($_POST['category_id']) : 0;
		$title = (isset($_POST['title']) && trim($_POST['title'])) ? trim($_POST['title']) : '';
		$ext_property = isset($_POST['ext_property']) ? $_POST['ext_property'] : '';
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$memo = (isset($_POST['memo']) && trim($_POST['memo'])) ? trim($_POST['memo']) : '';
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$pics = isset($_POST['pics']) ? $_POST['pics'] : '';
		$goods = isset($_POST['goods']) ? $_POST['goods'] : '';
		$pic = upload_one_file('article', 'pic');
		if (is_array($ext_property)) $ext_property = implode(',', $ext_property);
		if ($id>0) {
			$sql = "UPDATE {$this->tbp}article SET title='{$title}', pic='{$pic}', memo='{$memo}', content='{$content}', sort='{$sort}' ,
				status='{$status}', category_id='{$category_id}', ext_property='{$ext_property}'
				WHERE id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}article(title, pic, memo, content, sort, category_id, add_time, status, ext_property)
				VALUES('{$title}', '{$pic}', '{$memo}', '{$content}', '{$sort}', '{$category_id}', '".time()."', '{$status}', '{$ext_property}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		//文章图片
		$sql = "DELETE FROM {$this->tbp}article_pic WHERE article_id='{$id}'";
		$this->db->query($sql);
		if (is_array($pics)) {
			$sql = "INSERT INTO {$this->tbp}article_pic (article_id, pic, add_time) VALUES ";
			foreach ($pics as $k => $pic) {
				$sql .= "('{$id}', '{$pic}', '".time()."'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		//关联商品
		$sql = "DELETE FROM {$this->tbp}article_goods WHERE article_id='{$id}'";
		$this->db->query($sql);
		if (is_array($goods)) {
			$sql = "INSERT INTO {$this->tbp}article_goods (article_id, goods_id) VALUES ";
			foreach ($goods as $k => $g) {
				$sql .= "('{$id}', '{$g}'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		header("Location:?app=article&act=index");
		exit;
	}

	//delete
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}article WHERE id='{$id}'";
		if ($this->db->query($sql)) {
			$sql = "DELETE FROM {$this->tbp}article_pic WHERE article_id='{$id}'";
			$this->db->query($sql);
			//删除该商店被收藏
			$sql = "DELETE FROM {$this->tbp}favorite WHERE item_id='{$id}' AND type_id = 4";
			$this->db->query($sql);
		}
		header("Location:?app=article&act=index");
	}
	
	//添加评论回复
	public function reply_add() {
		$article_id = (isset($_POST['article_id']) && intval($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		$parent_id = (isset($_POST['parent_id']) && intval($_POST['parent_id'])) ? intval($_POST['parent_id']) : 0;
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		if ($article_id<=0) error('缺失文章id', -1, 1, true);
		if ($parent_id<=0) error('缺失父评论id', -1, 1, true);
		if (!$content) error('请填写回复内容', -1, 1, true);
		$time = time();
		$sql = "INSERT INTO {$this->tbp}article_comment (member_id, article_id, parent_id, content, ip, add_time) VALUES 
			('0', '{$article_id}', '{$parent_id}', '{$content}', '".$this->ip."', '{$time}')";
		$this->db->query($sql);
		$id = $this->db->insert_id;
		success(array('id'=>$id, 'member_name'=>'客服', 'content'=>$content, 'add_time'=>get_time_word($time)));
	}
	
	//删除评论回复
	public function reply_delete() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}article_comment WHERE id='{$id}'";
		$this->db->query($sql);
		success('ok');
	}

	//like
	public function likes(){
		$where = '';
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		if ($keyword) {
			$where .= " AND (m.name LIKE '%{$keyword}%' OR l.ip LIKE '%{$keyword}%')";
		}
		if ($article_id) {
			$where .= " AND (l.article_id LIKE '%{$article_id}%')";
		}
		$this->setpages(array());
		$sql = "SELECT l.*,m.name FROM {$this->tbp}article_like l LEFT JOIN {$this->tbp}member m ON l.member_id = m.id
			WHERE 1 {$where} ORDER BY l.id DESC";
		$val = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('val',$val);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('article_id',$article_id);
		$this->display();
	}
	//delete
	public function delete_like() {
		$id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$all = (isset($_GET['all']) && trim($_GET['all'])) ? intval($_GET['all']) : 0;
		$like = (isset($_GET['like']) && trim($_GET['like'])) ? intval($_GET['like']) : 0;
		if ($all == 1 && $id > 0) {
			$sql = "DELETE FROM {$this->tbp}article_like WHERE article_id = {$id}";
			if ($this->db->query($sql)) {
				$sql = "UPDATE {$this->tbp}article SET likes = 0 WHERE id = {$id}";
				$this->db->query($sql);
			} else {
				error('删除失败');
			}
		}
		if ($like > 0) {
			$sql = "DELETE FROM {$this->tbp}article_like WHERE id='{$like}'";
			if ($this->db->query($sql)) {
				$sql = "UPDATE {$this->tbp}article SET likes = likes-1 WHERE id='{$id}'";
				$this->db->query($sql);
			} else {
				error('删除失败');
			}
		}
		header("Location:?app=article&act=likes&article_id={$id}");
	}
	//commemt
	public function comment(){
		$where = '';
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) :'';
		if ($keyword) {
			$where .= " AND (m.name LIKE '%{$keyword}%' OR l.ip LIKE '%{$keyword}%')";
		}
		if ($article_id) {
			$where .= " AND (l.article_id LIKE '%{$article_id}%' )";
		}
		$this->setpages(array());
		$sql = "SELECT l.*,m.name FROM {$this->tbp}article_comment l LEFT JOIN {$this->tbp}member m ON l.member_id = m.id
			WHERE 1 {$where} ORDER BY l.id DESC";
		$val = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$this->smarty->assign('val',$val);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->smarty->assign('article_id',$article_id);
		$this->display();
	}
	//comment
	public function delete_comment() {
		$id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$all = (isset($_GET['all']) && trim($_GET['all'])) ? intval($_GET['all']) : 0;
		$comment = (isset($_GET['comment']) && trim($_GET['comment'])) ? intval($_GET['comment']) : 0;
		if ($all == 1 && $id > 0) {
			$sql = "DELETE FROM {$this->tbp}article_comment WHERE article_id = {$id}";
			if ($this->db->query($sql)) {
				$sql = "UPDATE {$this->tbp}article SET comments = 0 WHERE id = {$id}";
				$this->db->query($sql);
			} else {
				error('删除失败');
			}
		}
		if ($comment > 0) {
			$sql = "DELETE FROM {$this->tbp}article_comment WHERE id='{$comment}'";
			if ($this->db->query($sql)) {
				$sql = "UPDATE {$this->tbp}article SET comments = comments-1 WHERE id='{$id}'";
				$this->db->query($sql);
			} else {
				error('删除失败');
			}
		}
		header("Location:?app=article&act=comment&article_id={$id}");
	}
}
